﻿package plugin.social.game
{
    import com.core.*;
    import com.sh.game.consts.*;
    import com.sh.game.global.*;
    import com.sh.game.util.*;
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.geom.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import plugin.social.*;
    import plugin.social.consts.*;
    import uiresource.friend.*;

    public class SocialGame extends SocialUI
    {
        private var _instance:Social;
        private var _curPage:int;
        private var canCall:Boolean = true;
        private var _socialData:Object;
        private var _menu:MenuGame;
        private var _curSelect:int = -1;
        private var _selectArr:Array;
        private var careerArr:Array;
        private var _showState:Boolean = false;
        private var _socialFilter:SocialFilter;
        private var _selected:Box;
        private var _callTime:Number = 0;
        private var paiming:int;

        public function SocialGame(param1:Social)
        {
            this._selectArr = [];
            this.careerArr = ["", "战士", "法师", "道士"];
            this._instance = param1;
            return;
        }// end function

        override protected function initializeView() : void
        {
            var _loc_1:* = Control.create(SocialControl, this);
            ModelProxy.addObserver("social", _loc_1);
            this._socialFilter = new SocialFilter();
            var _loc_3:* = new MenuGame();
            this._menu = new MenuGame();
            this.addChild(_loc_3);
            this._menu.x = 400;
            this._menu.visible = false;
            img_head.x = 10;
            img_head.y = 30;
            var _loc_2:* = new Sprite();
            _loc_2.graphics.beginFill(16777215, 1);
            _loc_2.graphics.drawCircle(0, 0, 30);
            _loc_2.graphics.endFill();
            _loc_2.x = 58;
            _loc_2.y = 80;
            this.addChild(_loc_2);
            this.img_head.mask = _loc_2;
            this.initSet();
            return;
        }// end function

        private function initSet() : void
        {
            var _loc_1:* = UserData.getUserData();
            this.setImg(_loc_1.career + "_" + _loc_1.sex, img_head);
            nameLabel.text = _loc_1.name;
            cancerLabel.text = "职业：" + UIGlobel.getCareer(ModelProxy.getValue("data", "user.career"));
            levelLabel.text = "等级：" + _loc_1.level + "级";
            return;
        }// end function

        private function setImg(param1:String, param2:Image) : void
        {
            var _loc_3:* = "assets/images/head/" + param1 + ".png";
            _loc_3 = Config.getUrl(_loc_3, ResourceType.NORMAL);
            App.mloader3.loadBMD(_loc_3, 1, new Handler(this.onComp, [_loc_3, param2]));
            return;
        }// end function

        private function onComp(param1:String, param2:Image, param3:BitmapData) : void
        {
            param2.url = param1;
            return;
        }// end function

        public function initEvent() : void
        {
            btn_close.clickHandler = new Handler(this.doClose);
            ti_feel.addEventListener(FocusEvent.FOCUS_IN, this.focusIn);
            ti_feel.addEventListener(FocusEvent.FOCUS_OUT, this.focusOut);
            ti_feel.addEventListener(MouseEvent.MOUSE_DOWN, this.doStop);
            this.addEventListener(MouseEvent.MOUSE_DOWN, this.noFocus);
            btn_add.clickHandler = new Handler(this.openSearch);
            list_msg.mouseHandler = new Handler(this.msgMouse);
            btn1.clickHandler = new Handler(this.changeTitle, [0]);
            btn2.clickHandler = new Handler(this.changeTitle, [1]);
            btn3.clickHandler = new Handler(this.changeTitle, [2]);
            btn1.addEventListener(MouseEvent.ROLL_OVER, this.onRoll);
            btn1.addEventListener(MouseEvent.ROLL_OUT, this.onOut);
            return;
        }// end function

        protected function onRoll(event:MouseEvent) : void
        {
            var _loc_2:String = "";
            if (this.paiming)
            {
                _loc_2 = "您的等级排位：" + this.paiming;
            }
            else
            {
                _loc_2 = "您还没有好友";
            }
            ModelProxy.change("tips", "normal", {data:_loc_2, p:new Point(mouseX, mouseY)});
            return;
        }// end function

        protected function onOut(event:MouseEvent) : void
        {
            ModelProxy.change("tips", "state", "close");
            return;
        }// end function

        public function removeEvent() : void
        {
            btn_close.clickHandler = null;
            ti_feel.removeEventListener(FocusEvent.FOCUS_IN, this.focusIn);
            ti_feel.removeEventListener(FocusEvent.FOCUS_OUT, this.focusOut);
            ti_feel.removeEventListener(MouseEvent.MOUSE_DOWN, this.doStop);
            this.removeEventListener(MouseEvent.MOUSE_DOWN, this.noFocus);
            btn_add.clickHandler = null;
            list_msg.mouseHandler = null;
            return;
        }// end function

        private function msgMouse(event:MouseEvent, param2:int) : void
        {
            var _loc_3:Box = null;
            var _loc_4:Box = null;
            var _loc_5:Image = null;
            switch(event.type)
            {
                case MouseEvent.ROLL_OVER:
                {
                    _loc_3 = list_msg.getCell(param2);
                    _loc_4 = _loc_3.getChildByName("box_bg") as Box;
                    _loc_5 = _loc_4.getChildByName("img_bg") as Image;
                    _loc_5.filters = [this._socialFilter.getColorFilter(SocialFilter.LIGHT)];
                    break;
                }
                case MouseEvent.ROLL_OUT:
                {
                    _loc_3 = list_msg.getCell(param2);
                    _loc_4 = _loc_3.getChildByName("box_bg") as Box;
                    _loc_5 = _loc_4.getChildByName("img_bg") as Image;
                    _loc_5.filters = null;
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function deleteHandler() : void
        {
            if (this._selectArr.length != 0)
            {
                PluginManager.instance.sendToServer("social", CMDSocial.DeleteRela, this._selectArr);
            }
            else
            {
                ModelProxy.change("movetips", "error", "亲，请先勾选要删除的对象再进行删除！");
            }
            return;
        }// end function

        public function changeTitle(param1:int) : void
        {
            this._curPage = param1;
            this.setList();
            return;
        }// end function

        protected function doStop(event:MouseEvent) : void
        {
            event.stopPropagation();
            return;
        }// end function

        protected function noFocus(event:MouseEvent) : void
        {
            GlobalLayer.instance.root.focus = null;
            this._menu.visible = false;
            return;
        }// end function

        protected function focusIn(event:FocusEvent) : void
        {
            this._menu.visible = false;
            return;
        }// end function

        protected function focusOut(event:FocusEvent) : void
        {
            PluginManager.instance.sendToServer("social", CMDSocial.ChangeData, {param0:"xinqin", param1:ti_feel.text});
            return;
        }// end function

        private function doClose() : void
        {
            this._instance.close();
            this.removeEvent();
            return;
        }// end function

        public function updateMsg(param1:int, param2:Object) : void
        {
            var _loc_3:Object = null;
            var _loc_4:String = null;
            var _loc_5:Object = null;
            var _loc_6:Object = null;
            switch(param1)
            {
                case 1:
                {
                    for (_loc_4 in param2)
                    {
                        
                        _loc_5 = param2[_loc_4];
                        if (_loc_5.hasOwnProperty("rid"))
                        {
                            param2[_loc_4].rid = _loc_4;
                        }
                    }
                    this._socialData = param2;
                    this.setSelf();
                    this.setList();
                    break;
                }
                case 999:
                {
                    for each (_loc_6 in param2)
                    {
                        
                        if (_loc_6.state == -1)
                        {
                            if (this._socialData[_loc_6.rid])
                            {
                                if (this._socialData[_loc_6.rid].state != -1)
                                {
                                    WarningManager.show(102, [_loc_6.name]);
                                }
                            }
                        }
                        else if (_loc_6.state == 0)
                        {
                            WarningManager.show(103);
                        }
                        else if (_loc_6.state == -2)
                        {
                            WarningManager.show(101, [_loc_6.name]);
                        }
                        this._socialData[_loc_6.rid] = _loc_6;
                    }
                    this.setSelf();
                    this.setList();
                    break;
                }
                case CMDSocial.UpAndDown:
                {
                    _loc_3 = this._socialData[param2[0]];
                    if (!_loc_3)
                    {
                        return;
                    }
                    _loc_3.online = Boolean(param2[2]);
                    this.setSelf();
                    this.setList();
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function setSelf() : void
        {
            ti_feel.text = this._socialData.xinqin;
            return;
        }// end function

        public function setList() : void
        {
            var _loc_12:int = 0;
            var _loc_1:Array = [];
            this.getList(_loc_1, [1, 2]);
            var _loc_2:String = "我的好友【num/99】";
            if (_loc_1.length > 0)
            {
                this.paiming = this.getPaiMing(_loc_1);
            }
            this["btn1"].label = _loc_2.replace("num", _loc_1.length);
            var _loc_3:Array = [];
            this.getList(_loc_3, [-1]);
            var _loc_4:String = "我的对手【num/99】";
            this["btn2"].label = _loc_4.replace("num", _loc_3.length);
            var _loc_5:Array = [];
            this.getList(_loc_5, [-2]);
            var _loc_6:String = "我的黑名单【num/99】";
            this["btn3"].label = _loc_6.replace("num", _loc_5.length);
            var _loc_7:Array = [];
            switch(this._curPage)
            {
                case 0:
                {
                    _loc_7 = _loc_1;
                    break;
                }
                case 1:
                {
                    _loc_7 = _loc_3;
                    break;
                }
                case 2:
                {
                    _loc_7 = _loc_5;
                    break;
                }
                default:
                {
                    break;
                }
            }
            _loc_7.sortOn("index", Array.NUMERIC);
            _loc_7.sortOn("online", Array.DESCENDING);
            list_msg.array = _loc_7;
            list_msg.renderHandler = new Handler(this.msgRender);
            var _loc_8:int = 147;
            var _loc_9:int = 26;
            var _loc_10:int = 246;
            var _loc_11:int = 0;
            while (_loc_11 < 3)
            {
                
                if (_loc_11 <= this._curPage)
                {
                    this["btn" + (_loc_11 + 1)].y = _loc_8 + _loc_9 * _loc_11;
                }
                else
                {
                    _loc_12 = list_msg.array.length;
                    if (_loc_12 > 4)
                    {
                        _loc_12 = 4;
                    }
                    this["btn" + (_loc_11 + 1)].y = _loc_8 + _loc_9 * _loc_11 + _loc_10;
                }
                _loc_11++;
            }
            list_msg.y = this["btn" + (this._curPage + 1)].y + 26;
            this._selectArr.length = 0;
            return;
        }// end function

        private function msgRender(param1:Component, param2:int) : void
        {
            var _loc_3:Object = null;
            var _loc_4:Label = null;
            var _loc_5:int = 0;
            var _loc_6:Box = null;
            var _loc_7:Image = null;
            var _loc_8:Image = null;
            var _loc_9:Label = null;
            var _loc_10:Image = null;
            var _loc_11:int = 0;
            var _loc_12:Label = null;
            var _loc_13:LinkButton = null;
            if (param2 < list_msg.length)
            {
                _loc_3 = list_msg.array[param2];
                _loc_4 = param1.getChildByName("play_name") as Label;
                _loc_4.text = _loc_3.name;
                for each (_loc_5 in this._selectArr)
                {
                    
                    if (_loc_3.rid == _loc_5)
                    {
                        break;
                    }
                }
                _loc_4.dataSource = {rid:_loc_3.rid};
                _loc_6 = param1.getChildByName("box_bg") as Box;
                _loc_6.dataSource = {index:param2};
                _loc_6.addEventListener(MouseEvent.CLICK, this.bgMouse, false, 0, true);
                _loc_6.addEventListener(MouseEvent.ROLL_OVER, this.bgMouse, false, 0, true);
                _loc_6.addEventListener(MouseEvent.ROLL_OUT, this.bgMouse, false, 0, true);
                _loc_6.addEventListener(MouseEvent.CLICK, this.bgMouse, false, 0, true);
                _loc_7 = _loc_6.getChildByName("img_bg") as Image;
                _loc_8 = _loc_6.getChildByName("img_head") as Image;
                this.setImg(_loc_3.career + "_" + _loc_3.sex, _loc_8);
                _loc_9 = param1.getChildByName("txt_level") as Label;
                _loc_9.text = "等级:" + _loc_3.level + "级";
                _loc_10 = param1.getChildByName("img_vip") as Image;
                _loc_11 = SocialUtil.checkVipLevel(_loc_3.vip, _loc_3.viptime, _loc_3.viplevel);
                if (_loc_11 > 0)
                {
                    _loc_10.visible = true;
                }
                else
                {
                    _loc_10.visible = false;
                }
                if (_loc_3.online)
                {
                    param1.filters = null;
                }
                else
                {
                    param1.filters = [this._socialFilter.getColorFilter(SocialFilter.GRAVE)];
                }
                _loc_12 = param1.getChildByName("ko_count") as Label;
                _loc_13 = param1.getChildByName("btn_call") as LinkButton;
                if (this._curPage == 1)
                {
                    _loc_13.label = "追踪";
                    if (_loc_3.killednum)
                    {
                        _loc_12.visible = true;
                        _loc_12.text = "累计杀你：" + _loc_3.killednum + "次";
                    }
                }
                else if (this._curPage == 0)
                {
                    _loc_13.label = "召唤";
                    _loc_12.visible = false;
                }
                if (this.canCall && _loc_3.hasOwnProperty("state"))
                {
                    if (_loc_3.state == 1 || _loc_3.state == -1)
                    {
                        _loc_13.visible = true;
                        _loc_13.clickHandler = new Handler(this.zhaohuan, [_loc_3]);
                    }
                    else
                    {
                        _loc_13.visible = false;
                    }
                }
                else
                {
                    _loc_13.visible = false;
                }
            }
            return;
        }// end function

        private function zhaohuan(param1:Object) : void
        {
            if (this._curPage == 0)
            {
                this.openTrace(1, param1);
            }
            else if (this._curPage == 1)
            {
                this.openTrace(2, param1);
            }
            return;
        }// end function

        private function cbHandler(param1:CheckBox) : void
        {
            var _loc_2:* = param1.dataSource.rid;
            if (param1.selected)
            {
                this._selectArr.push(_loc_2);
            }
            else
            {
                this._selectArr.splice(this._selectArr.indexOf(_loc_2), _loc_2);
            }
            return;
        }// end function

        protected function bgMouse(event:MouseEvent) : void
        {
            var _loc_5:Object = null;
            var _loc_2:* = event.currentTarget.dataSource.index;
            var _loc_3:* = list_msg.getCell(_loc_2);
            var _loc_4:* = _loc_3.getChildByName("box_bg") as Box;
            switch(event.type)
            {
                case MouseEvent.CLICK:
                {
                    if (_loc_2 == this._curSelect)
                    {
                        var _loc_6:* = !this._showState;
                        this._showState = !this._showState;
                        this._menu.visible = _loc_6;
                    }
                    else
                    {
                        var _loc_6:Boolean = true;
                        this._menu.visible = true;
                        this._showState = _loc_6;
                    }
                    this._menu.setMenu(this._curPage, _loc_3.dataSource);
                    this._curSelect = _loc_2;
                    this._menu.x = mouseX;
                    this._menu.y = mouseY;
                    if (this._selected)
                    {
                        this._selected.filters = null;
                    }
                    _loc_4.filters = [new GlowFilter(6737151, 0.5, 6, 6, 2, 1, true)];
                    this._selected = _loc_4;
                    ModelProxy.change("tips", "state", "close");
                    break;
                }
                case MouseEvent.ROLL_OVER:
                {
                    if (_loc_3.dataSource)
                    {
                        _loc_5 = _loc_3.dataSource;
                        if (_loc_5.xinqin)
                        {
                            if (_loc_5.xinqin != "点击此处输入心情")
                            {
                                ModelProxy.change("tips", "normal", {data:_loc_5.xinqin, p:new Point(mouseX, mouseY)});
                            }
                            else
                            {
                                ModelProxy.change("tips", "normal", {data:"该玩家暂时还没有心情！", p:new Point(mouseX, mouseY)});
                            }
                        }
                    }
                    break;
                }
                case MouseEvent.ROLL_OUT:
                {
                    ModelProxy.change("tips", "state", "close");
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function openTrace(param1:int, param2:Object) : void
        {
            var _loc_3:Object = null;
            if (param2.online)
            {
                _loc_3 = new Object();
                _loc_3.type = param1;
                _loc_3.data = param2;
                PluginManager.instance.openPlugin("tracing", UIGlobel.pointsDic["social"], _loc_3);
            }
            else if (param1 == 1)
            {
                ModelProxy.change("movetips", "error", "亲，玩家不在线无法进行召唤！");
            }
            else
            {
                ModelProxy.change("movetips", "error", "亲，玩家不在线无法进行追踪！");
            }
            return;
        }// end function

        private function openSearch() : void
        {
            PluginManager.instance.openPlugin("findFriend", UIGlobel.pointsDic["social"], null);
            return;
        }// end function

        private function getList(param1:Array, param2:Array) : void
        {
            var _loc_3:Object = null;
            for each (_loc_3 in this._socialData)
            {
                
                if (_loc_3.hasOwnProperty("state"))
                {
                    if (param2.indexOf(_loc_3.state) >= 0)
                    {
                        param1.push(_loc_3);
                    }
                }
            }
            return;
        }// end function

        private function getPaiMing(param1:Array) : int
        {
            var _loc_2:* = ModelProxy.getValue("data", "user");
            var _loc_3:int = 1;
            var _loc_4:int = 0;
            while (_loc_4 < param1.length)
            {
                
                if (param1[_loc_4].level >= _loc_2.level)
                {
                    _loc_3 = _loc_3 + 1;
                }
                _loc_4++;
            }
            return _loc_3;
        }// end function

    }
}
